<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use backend\utils\XHtml;
use kartik\file\FileInput;
use yii\helpers\Url;

/* @var $this yii\web\View */
/* @var $model common\models\Article */

$this->title = $model->isNewRecord ? '新建素材' : '更新素材';
$this->params['breadcrumbs'][] = ['label' => '素材管理', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
    <div class="page-create">
        <div class="box box-primary">
            <div class="box-body">

                <?= $this->render('/site/error-summary', ['model' => $model]) ?>

                <?php $form = ActiveForm::begin([
                    'options' => ['class' => 'form-horizontal'],
                    'fieldConfig' => [
                        'template' => "{label}\n<div class=\"col-sm-8\">{input}</div>\n<div class=\"col-sm-2\">{hint}\n{error}</div>",
                        'labelOptions' => ['class ' => 'col-sm-2 control-label'],
                    ]
                ]); ?>
                <?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
                <?= $form->field($model, 'author') ?>
                <?= $form->field($model, 'intro')->textarea() ?>
                <?= $form->field($model, 'face')->fileInput(['accept' => 'image/*']) ?>
                <?php if ($model->face) { ?>
                    <?= Html::beginTag('div', ['class' => 'form-group']) ?>
                    <?= Html::label('已上传封面', '', ['class' => 'col-sm-2 control-label']) ?>
                    <?= Html::beginTag('div', ['class' => 'col-sm-10']) ?>
                    <?= Html::img(Yii::getAlias('@frontendUrl') . $model->face, ['width' => '200']) ?>
                    <?= Html::endTag('div') ?>
                    <?= Html::endTag('div') ?>
                <?php } ?>
                <div class="form-group">
                    <label class="col-sm-2 control-label">组图</label>
                    <div class="col-sm-7">
                        <?php if ($model->photos) { ?>
                            <?= Html::button('<i class="fa fa-minus"></i>', ['id' => 'photosBt']) ?>
                        <?php } else { ?>
                            <?= Html::button('<i class="fa fa-plus"></i>', ['id' => 'photosBt']) ?>
                        <?php } ?>
                    </div>
                </div>
                <?= $form->field($model, 'photos')->label("&nbsp;")->widget(FileInput::classname(), [
                    'options' => ['accept' => 'image/*', 'multiple' => true],
                    'pluginOptions' => [
                        'previewFileType' => 'image',
                        'initialPreview' => $p1,
                        'initialPreviewConfig' => $p2,
                        'initialPreviewAsData' => true,
                        'uploadUrl' => Url::to(['upload-photos', 'modelName' => 'Article']),
                        'uploadAsync' => true,
                        'minFileCount' => 0,
                        'maxFileCount' => 9,
                        'browseOnZoneClick' => true,
                        'showRemove' => false,
                        'fileActionSettings' => [
                            'showUpload' => false,
                        ],
                    ],
                    'pluginEvents' => [
                        "fileuploaded" => "function (event, data, id, index) {
                            var photo = $('.field-article-photos').find('.col-sm-8').children(':first');
                            photo.val(photo.val()+data.response.initialPreviewConfig[0].key+'|');
                        }",
                        "filecleared" => "function (event, data, id, index) {
                            $('.field-article-photos').find('.col-sm-8').children(':first').val('');
                        }"
                    ],
                ]) ?>
                <?= $form->field($model, 'is_link')->dropDownList(['0' => '图片模式', '1' => '外链模式'])->label('展现方式') ?>
                <?= $form->field($model, 'link')->textInput(['maxlength' => true]) ?>
                <?= $form->field($model, 'content')->widget('pjkui\kindeditor\KindEditor', ['clientOptions' => ['allowFileManager' => 'true', 'allowUpload' => 'true']]) ?>

                <div class="form-group">
                    <label class="col-sm-2 control-label"></label>
                    <?= XHtml::submitButton($model->isNewRecord ? '创建' : '更新') ?>&nbsp;&nbsp;
                    <?= XHtml::backButton() ?>&nbsp;&nbsp;
                    <?= XHtml::resetButton() ?>
                </div>
                <?php ActiveForm::end(); ?>
            </div>
        </div>
    </div>
<?php
$script = <<<JS
    if ('$model->is_link'=='1'){
        $(".field-article-content").hide();
    }else {
        $(".field-article-link").hide();
    }
    $("#article-is_link").change(function() {
        if($(this).val()=='1'){
            $(".field-article-content").hide();
            $(".field-article-link").show();
        }else {
            $("#article-link").val('');
            $(".field-article-link").hide();
            $(".field-article-content").show();
        }
    });
    
    if ('$model->photos'!=''){
        $('.field-article-photos').find('.col-sm-8').children(':first').val('$model->photos|');
    }else {
        $(".field-article-photos").hide();
    }
    $('#photosBt').click(function() {
        if ($(this).find('.fa-plus').length>0){
            $('.field-article-photos').slideDown();
            $(this).find('i').removeClass('fa-plus').addClass('fa-minus');
        }else {
            $('.field-article-photos').slideUp();
            $(this).find('i').removeClass('fa-minus').addClass('fa-plus');
        }
    });
JS;
$this->registerJs($script);
?>